﻿using System;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using MammothMicro.SmtpGuardFilter.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MammothMicro.SmtpGuardFilter.Filter;
using MammothMicro.SMTPGuard.Filter;

namespace SmtpGuardTest
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class FilterTest
    {
        #region Variables


        private TestContext testContextInstance;

        #endregion
        public FilterTest()
        {
           // messageSpool = new MemoryMessageSpool();
        }
        #region SetUp/TearDown

        [TestInitialize]
        public void Setup()
        {
            var filterManager = new FilterManager(TimeSpan.FromHours(1), new NullMailStore());
            FilterManager.BlockNullSender = true;
            FilterManager.BlockNullSubject = true;

        }


        [TestCleanup]
        public void Teardown()
        {
            
        }

        #endregion
       
     
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void AllowedAttachmentTest()
        {
            var email = new MailMessage("test@yahoo.com", "ivan@mammothmicro.com");
            email.Subject = "test";
            email.Body = "test";
            var att = new Attachment(@"c:\reflector.zip");
            email.Attachments.Add(att);
            Assert.IsTrue(FilterManager.AttachmentFilter.Accept(email));

            var attachment = new Attachment(@"c:\filters.txt");
            email.Attachments.Add(attachment);
            Assert.IsFalse(FilterManager.AttachmentFilter.Accept(email));

        }

        [TestMethod]
        public void BlockedEmailFilterTestBlocked()
        {
            var msg = new MailMessage("aa@yahoo.com","ivan@mammothmicro.com");
            Assert.IsFalse(FilterManager.BlockedEmailFilter.Accept(msg));
        }

        [TestMethod]
        public void BlockedEmailFilterTestAllowed()
        {
            var msg1 = new MailMessage("hhh@yahoo.com", "Ivan@mammothmicro.com");
            Assert.IsTrue(FilterManager.BlockedEmailFilter.Accept(msg1));
        }

        [TestMethod]
        public void BlockedIpFilterTestAllowed()
        {
            var msg = new MailMessage();
            msg.Headers.Add(@"Received", @"from by2acnmsa01.alerts.live.com (207.46.117.142) by EXCHANGE2010.MammothMicro.local (192.168.10.227) with Microsoft SMTP Server id 14.0.639.21; Thu, 26 Nov 2009 08:33:23 -0800");
            msg.Headers.Add(@"Received", @"from mail pickup service by by2acnmsa01.alerts.live.com with Microsoft SMTPSVC; Thu, 26 Nov 2009 08:34:13 -0800");

            var filter = new BlockedIpListFilter();
            filter.Reload();
            Assert.IsTrue(filter.Accept(msg));
            
        }

        [TestMethod]
        public void BlockedIpFilterTestBlocked()
        {
            var msg = new MailMessage();
            msg.Headers.Add(@"Received", @"from mail pickup service by (1.1.1.1) with Microsoft SMTPSVC; Thu, 26 Nov 2009 08:34:13 -0800");
            Assert.IsFalse(FilterManager.BlockedIpFilter.Accept(msg));
        }

        [TestMethod]
        public void BlockedKeywordTestAllowed()
        {
            var msg = new MailMessage
                          {
                              Body = "dddddddddd ffffffffffffff gggggggggg hhhhhhhhhhhh"
                          };
            Assert.IsTrue(FilterManager.BlockedKeywordFilter.Accept(msg));
        }

        [TestMethod]
        public void BlockedKeywordTestBlocked()
        {
           var msg = new MailMessage
                         {
                             Body = "dddddddddd ffffffffffffff sex gggggggggg hhhhhhhhhhhh"
                         };
            Assert.IsFalse(FilterManager.BlockedKeywordFilter.Accept(msg));
        }

        [TestMethod]
        public void RblFilterTest()
        {
            var list = new string[1];
            list[0] = "zen.spamhaus.org";
            var rbl = new RblVerifyIP("4.2.2.2", list);
            Assert.IsFalse(rbl.BlackList.IsListed);
            rbl = new RblVerifyIP("71.216.207.187",list);
            Assert.IsFalse(rbl.BlackList.IsListed);
            rbl = new RblVerifyIP("162.97.143.0",list);
            Assert.IsTrue(rbl.BlackList.IsListed);

        }

        [TestMethod]
        public void DomainWhiteListTestAllowed()
        {
            var msg = new MailMessage("ivan@mammothmicro.com","Ivan@mammothmicro.com");
            Assert.IsTrue(FilterManager.DomainWhiteListFilter.Accept(msg));
        }

        [TestMethod]
        public void DomainWhiteListBlocked()
        {
            var msg = new MailMessage("ivan@ndomain.com", "Ivan@mammothmicro.com");
            Assert.IsFalse(FilterManager.DomainWhiteListFilter.Accept(msg));
        }

        [TestMethod]
        public void EmailWhiteListTestAllowed()
        {
            var msg = new MailMessage("aaa@yahoo.com", "test@mammothmicro.com");
            Assert.IsTrue(FilterManager.EmailWhiteListFilter.Accept(msg));
        }

        [TestMethod]
        public void EmailWhiteListTestBlocked()
        {
            var msg = new MailMessage("a@yahoo.com", "test@mammothmicro.com");
            Assert.IsFalse(FilterManager.EmailWhiteListFilter.Accept(msg));
        }

        [TestMethod]
        public void LocalRecipientTestAllowed()
        {
            var msg = new MailMessage("postmaster@mammothmicro.com", "webmaster@mammothmicro.com");
            Assert.IsTrue(FilterManager.LocalRecipientFilter.Accept(msg));
        }

        [TestMethod]
        public void LocalRecipientTestBlocked()
        {
            var msg = new MailMessage("ivan@mammothmicro.com", "blocked@mammothmicro.com");
            Assert.IsFalse(FilterManager.LocalRecipientFilter.Accept(msg));
        }

        [TestMethod]
        public void NullSenderTestAllowed()
        {

            var msg = new MailMessage("a@aa.com","a@aa.com");
            Assert.IsTrue(FilterManager.NullSenderFilter.Accept(msg));
        }

        [TestMethod]
        public void NullSubjectTestAllowed()
        {
            FilterManager.BlockNullSubject = true;
            var msg = new MailMessage(){Subject = "Hello"};
            Assert.IsTrue(FilterManager.NullSubjectFilter.Accept(msg));
        }

        [TestMethod]
        public void NullSubjectTestBlocked()
        {
            FilterManager.BlockNullSubject = true;
            var msg = new MailMessage() ;
            Assert.IsFalse(FilterManager.NullSubjectFilter.Accept(msg));
        }

        [TestMethod]
        public void TrustedIpTestAllowed()
        {
            var msg = new MailMessage();
            msg.Headers.Add(@"Received", @"from by2acnmsa01.alerts.live.com (2.2.2.2) by EXCHANGE2010.MammothMicro.local (192.168.10.227) with Microsoft SMTP Server id 14.0.639.21; Thu, 26 Nov 2009 08:33:23 -0800");
  
            Assert.IsTrue(FilterManager.TrustedIpFilter.Accept(msg));
        }

        [TestMethod]
        public void MailFilterFlowTestAllow()
        {
            var msg = new MailMessage("test@yahoo.com","Ivan@mammothmicro.com");
            msg.Body = "Hello how are you?";
            msg.Subject = "Hello";
            
            var mf = new MailFilterFlow();
            Assert.IsTrue(mf.Execute(msg));
        }

        [TestMethod]
        public void MailFilterFlowTestBlock()
        {
            var msg = new MailMessage("test@yahoo.com", "Ivan@mammothmicro.com");
            msg.Body = "Hello how are you?";
            msg.Subject = "";

            var mf = new MailFilterFlow();
            Assert.IsFalse(mf.Execute(msg));
        }

    }
}
